package cn.findviewbydream.evil.main.service.impl;

import cn.findviewbydream.evil.main.enums.AccessType;
import cn.findviewbydream.evil.main.service.IndexService;
import cn.findviewbydream.evil.main.utils.CheckMobile;
import cn.findviewbydream.evil.main.utils.DataUtil;
import cn.findviewbydream.evil.main.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/********************************************
 * 工程名：HebeuCS
 * 包名：cn.findviewbydream.evil.main.service.impl
 * 时间：16-10-30 下午9:57
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：IndexService实现类
 ********************************************/
@Service("indexService")
public class IndexServiceImpl implements IndexService
{
    private static Logger logger = LoggerFactory.getLogger(IndexServiceImpl.class);
    private String loginfo;

    @Override
    public AccessType isMobileORie(HttpServletRequest request, HttpSession session)
    {
        loginfo = "";
        loginfo += Utils.getIpAddress(request);

        String agent = request.getHeader("User-Agent").toLowerCase();
        if (isMobile(request, session))
        {
            log(loginfo);
            return AccessType.MOBILE;
        }
        else if (isBrowserAllow(getBrowserName(agent)))
        {
            log(loginfo);
            return AccessType.PC;
        }
        else
        {
            log(loginfo);
            return AccessType.PC_IE;
        }
    }

    /**
     * 打印日志(仅限浏览器第一次访问首页)
     * 访问次数+1
     *
     * @param loginfo loginfo
     */
    private void log(String loginfo)
    {
        if (loginfo.contains(">!>"))
        {
            logger.info(loginfo);
            int accessTimes = DataUtil.getAccessTimes();
            DataUtil.setAccessTimes(++accessTimes);
        }
    }

    /**
     * 判断移动端访问
     *
     * @param request request
     * @param session session
     * @return 移动端访问
     */
    private boolean isMobile(HttpServletRequest request, HttpSession session)
    {
        boolean isFromMobile = false;

        //检查是否已经记录访问方式（移动端或pc端）
        if (null == session.getAttribute("ua"))
        {
            //获取ua，用来判断是否为移动端访问
            String userAgent = request.getHeader("USER-AGENT").toLowerCase();
            isFromMobile = CheckMobile.check(userAgent);
            //判断是否为移动端访问
            if (isFromMobile)
            {
                loginfo += ">!>移动端访问";
                session.setAttribute("ua", "mobile");
            }
            else
            {
                loginfo += ">!>pc端访问>>";
                session.setAttribute("ua", "pc");
            }
        }
        else
            isFromMobile = session.getAttribute("ua").equals("mobile");
        return isFromMobile;
    }

    /**
     * 判断浏览器版本是否放行
     *
     * @param version 版本
     * @return 是否放行
     */
    private boolean isBrowserAllow(String version)
    {
        loginfo += "浏览器：" + version;
        return !version.contains("ie") || Integer.valueOf(version.replace("ie", "")) >= 10;
    }

    /**
     * 获取浏览器版本
     *
     * @param agent agent
     * @return 浏览器版本
     */
    private String getBrowserName(String agent)
    {
        if (agent.indexOf("msie 7") > 0)
            return "ie7";
        else if (agent.indexOf("msie 8") > 0)
            return "ie8";
        else if (agent.indexOf("msie 9") > 0)
            return "ie9";
        else if (agent.indexOf("msie 10") > 0)
            return "ie10";
        else if (agent.indexOf("msie") > 0)
            return "ie";
        else if (agent.indexOf("opera") > 0)
            return "opera";
        else if (agent.indexOf("opera") > 0)
            return "opera";
        else if (agent.indexOf("firefox") > 0)
            return "firefox";
        else if (agent.indexOf("webkit") > 0)
            return "webkit";
        else if (agent.indexOf("gecko") > 0 && agent.indexOf("rv:11") > 0)
            return "ie11";
        else
            return "Others";
    }
}


























